package com.example.lizhixin.object_three_team;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.example.lizhixin.object_three_team.Base.BaseActivity;
import com.example.lizhixin.object_three_team.Uitls.FragmentFactory;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements BottomNavigationBar.OnTabSelectedListener {


    @BindView(R.id.main_Frame)
    FrameLayout mainFrame;
    @BindView(R.id.main_bot_ber)
    BottomNavigationBar mainBotBer;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        mainBotBer = findViewById(R.id.main_bot_ber);
        initbottom();
        initFragment();
    }

    private void initFragment() {
        Fragment fragment = FragmentFactory.getFragment(0);
        getSupportFragmentManager().beginTransaction().add(R.id.main_Frame, fragment, "0").commit();
    }

    private void initbottom() {

        mainBotBer.setBarBackgroundColor(R.color.mian_btn);
        mainBotBer.addItem(new BottomNavigationItem(R.mipmap.game, "精选"))
                .addItem(new BottomNavigationItem(R.mipmap.at_off, "游戏"))
                .addItem(new BottomNavigationItem(R.mipmap.special_topic, "社区"))
                .addItem(new BottomNavigationItem(R.mipmap.button, "我的"))
                .setFirstSelectedPosition(0)
                .initialise();
        mainBotBer.setTabSelectedListener(this);
    }

    @Override
    public void onTabSelected(int position) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.hide(FragmentFactory.getFragment(0));
        transaction.hide(FragmentFactory.getFragment(1));
        transaction.hide(FragmentFactory.getFragment(2));
        transaction.hide(FragmentFactory.getFragment(3));
        Fragment fragment = FragmentFactory.getFragment(position);
        if (!fragment.isAdded()) {
            transaction.add(R.id.main_Frame, fragment, position + "");
        }
        transaction.show(fragment);
        transaction.commit();
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }
}
